package com.dingding.service.core.special.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.daox.IBaseDaoX;
import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.base.daox.ISQLBaseDao;
import com.dingding.common.core.beans.special.SpecialDrivingRecord;
import com.dingding.facade.user.common.dto.special.driver.ResOrderMileageCost;
import com.dingding.service.core.common.dao.impl.DriverDaoImpl;
import com.dingding.service.core.special.dao.ISpecialDrivingRecordDao;
import com.dingding.service.core.special.sql.SpecialDrivingRecordSQL;

/**
 * 
 * @ClassName: ISpecialDrivingRecordDao 
 * @Description: 车主行车记录DAO接口实现类
 * @author YHQ
 * @date 2016年12月8日 上午9:46:02
 * @since V2.5.7
 * 
 */
@Component("specialDrivingRecordDao")
public class SpecialDrivingRecordDaoImpl implements ISpecialDrivingRecordDao{

	private Logger log = Logger.getLogger(DriverDaoImpl.class);

	@Resource(name = "SQLBaseDao")
	private ISQLBaseDao sqlBaseDao;

	@Resource(name = "criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;

	@Resource(name = "baseDaoX")
	private IBaseDaoX baseDaoX;
	
	/**
	 * 
	 * @Title: getSpecialDrivingRecored 
	 * @Description: 获取专车司机行车记录表
	 * @param userId	用户Id
	 * @param orderId	订单Id
	 * @author YHQ 
	 * @since V2.5.7
	 * @return SpecialDrivingRecord    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public SpecialDrivingRecord getSpecialDrivingRecored(String userId,String orderId) {
		
		try {
			
			//获取司机行车记录信息
			List<SpecialDrivingRecord> specialDrivingRecords = sqlBaseDao.listSqlToCustomBean(SpecialDrivingRecord.class, SpecialDrivingRecordSQL.GET_SPECIAL_DRIVING_RECORD, userId,orderId);
		
			// 返回最近的一条记录
			if(specialDrivingRecords != null && specialDrivingRecords.size() > 0){
				return specialDrivingRecords.get(0);
			}
			
		} catch (Exception e) {
			log.error("获取专车司机行车记录时出现异常   类: " + this.getClass() + " getSpecialDrivingRecored()", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: saveSpecialDrivingRecored 
	 * @Description: 保存专车司机行车记录表
	 * @param specialDrivingRecord 司机行车记录对象
	 * @author YHQ 
	 * @since V2.5.7
	 * @return boolean true:成功  false:失败
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean saveSpecialDrivingRecored(SpecialDrivingRecord specialDrivingRecord) {
		
		try {
			
			// 保存行车信息
			baseDaoX.save(specialDrivingRecord);
			
			return true;
		} catch (Exception e) {
			log.error("保存行车记录时出现异常   类：" + this.getClass() + " saveSpecialDrivingRecored()",e);
		}
		
		return false;
	}

	/**
	 * 
	 * @Title: getOrderMileageCost 
	 * @Description: 实时计算订单里程以及价格
	 * @param userId 用户Id
	 * @param orderId	订单Id
	 * @author YHQ 
	 * @since V2.5.7
	 * @return ResOrderMileageCost    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public ResOrderMileageCost getOrderMileageCost(String userId, String orderId) {
		
		try {
			return sqlBaseDao.sqlToCustomBeanUnique(ResOrderMileageCost.class, SpecialDrivingRecordSQL.GET_ORDER_MILEAGE_COST, orderId,userId);
		} catch (Exception e) {
			log.error("实时计算订单里程以及价格时出现异常   类：" + this.getClass() + " getOrderMileageCost()", e);
		}
		
		return null;
	}

}
